#include "LCD_CTRL_LIB.h"
#include "LCD_CFG.h"
void LCD_Hard_Init(void)
{
    LCD_GPIO_INIT();
#ifdef LCD_SPI
    LCD_SPI_INIT(LCD_SPI_SELECT);
#endif //LCD_SPI
#if(LCD_USE_DMA==1)
    Dma_Init();
#endif
    LCD_Rst();
    LCD_Reg_Config();
}

void LCD_OpenWindow ( uint16_t usX, uint16_t usY, uint16_t usWidth, uint16_t usHeight )
{
    LCD_Write_Cmd ( 0x2a ); 				 /* 设置X坐标 */
    {
        uint8_t data1[] = {usX >> 8, usX & 0xFF, usWidth >> 8, usWidth & 0xFF};
        LCD_Write_Data(data1, sizeof(data1));
    }
    LCD_Write_Cmd ( 0x2b ); 			     /* 设置Y坐标*/
    {
        uint8_t data2[] = {usY >> 8, usY & 0xFF, usHeight >> 8, usHeight & 0xFF};
        LCD_Write_Data(data2, sizeof(data2));
    }
   LCD_Write_Cmd ( 0x2c );
}

void LCD_FillColor(uint16_t LCD_Color)
{
    uint32_t i;
    LCD_OpenWindow(0,0,LCD_WIDTH,LCD_HEIGHT);
    LCD_Write_Cmd(0x2c);
    for ( i = 0; i <LCD_WIDTH*LCD_HEIGHT ; i ++ )
    {
        LCD_Write_Sing_Data((LCD_Color>>8)&0x00ff);
        LCD_Write_Sing_Data(LCD_Color&0x00ff);
    }
}
void LCD_Write_Cmd(uint8_t LCD_Cmd)//写命令只支持软件写
{
#ifdef LCD_SPI
    LCD_DC_L
    LCD_CS_L
    SPI_I2S_SendData(LCD_SPI_SELECT,LCD_Cmd);
    while(SPI_I2S_GetFlagStatus(LCD_SPI_SELECT,SPI_I2S_FLAG_TXE)==0);
    while(SPI_I2S_GetFlagStatus(LCD_SPI_SELECT,SPI_I2S_FLAG_RXNE)==0);
    LCD_SPI_SELECT->DR;
    LCD_CS_H
#endif
#ifdef LCD_8080

#endif
#ifdef LCD_QSPI

#endif

}
void LCD_Write_Data(uint8_t *LCD_Data_Buffer,uint32_t LCD_Data_Len)
{
    uint32_t i;
#if(LCD_USE_DMA==1)
    uint32_t LCD_DMA_65535,LCD_DMA_1;
    uint8_t *Data_Addr=LCD_Data_Buffer;
#endif //LCD_USE_DMA
    LCD_DC_H
    LCD_CS_L
#ifdef LCD_SPI
#if(LCD_USE_DMA==0)
    for(i=0; i<LCD_Data_Len; i++)
    {
        SPI_I2S_SendData(LCD_SPI_SELECT,LCD_Data_Buffer[i]);
        while(SPI_I2S_GetFlagStatus(LCD_SPI_SELECT,SPI_I2S_FLAG_TXE)==0);
        while(SPI_I2S_GetFlagStatus(LCD_SPI_SELECT,SPI_I2S_FLAG_RXNE)==0);
        LCD_SPI_SELECT->DR;
    }
#endif //LCD_USE_DMA==0
#if(LCD_USE_DMA==1)
    LCD_DMA_65535=LCD_Data_Len/65535;
    LCD_DMA_1=LCD_Data_Len%65535;
    if(LCD_DMA_65535!=0)
    {
        for(i=0; i<LCD_DMA_65535; i++)
        {
            Dma_Send_Data((uint32_t)Data_Addr,65535);
            Data_Addr+=65535;
        }
    }
    if(LCD_DMA_1!=0)
    {
        Dma_Send_Data((uint32_t)Data_Addr,LCD_DMA_1);
    }
#endif //LCD_USE_DMA==1
#endif //LCD_SPI

#ifdef LCD_8080
#if(LCD_USE_DMA==0)
#endif //LCD_USE_DMA==0
#if(LCD_USE_DMA==1)
#endif //LCD_USE_DMA==1
#endif
#ifdef LCD_QSPI
#if(LCD_USE_DMA==0)
#endif //LCD_USE_DMA==0
#if(LCD_USE_DMA==1)
#endif //LCD_USE_DMA==1
#endif
    LCD_CS_H
}
void LCD_Write_Sing_Data (uint8_t LCD_Data)
{
#ifdef LCD_SPI
    LCD_DC_H
    LCD_CS_L
    SPI_I2S_SendData(LCD_SPI_SELECT,LCD_Data);
    while(SPI_I2S_GetFlagStatus(LCD_SPI_SELECT,SPI_I2S_FLAG_TXE)==0);
    while(SPI_I2S_GetFlagStatus(LCD_SPI_SELECT,SPI_I2S_FLAG_RXNE)==0);
    LCD_SPI_SELECT->DR;
    LCD_CS_H
#endif //LCD_SPI

#ifdef LCD_8080

#endif //LCD_8080
#ifdef LCD_QSPI

#endif //LCD_QSPI
}


void Lcd_Enter_Sleep_Mode(void)
{
    LCD_Write_Sing_Data(0x28);
}

void Dma_Send_Data(uint32_t data,uint32_t Data_Len)
{
    if(LCD_SPI_SELECT==SPI1)
    {
        DMA_Cmd(DMA1_Channel3, DISABLE);
        SPI_I2S_DMACmd(LCD_SPI_SELECT,SPI_I2S_DMAReq_Tx,ENABLE);
        DMA1_Channel3->CMAR=(uint32_t)data;
        DMA1_Channel3->CPAR=(uint32_t)&(LCD_SPI_SELECT->DR);
        DMA1_Channel3->CNDTR=Data_Len;
        LCD_CS_L
        LCD_DC_H
        DMA_Cmd(DMA1_Channel3, ENABLE);

        while (DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET);//等待转运完成
        while (SPI_I2S_GetFlagStatus(LCD_SPI_SELECT,SPI_I2S_FLAG_TXE) == RESET);//等待转运完成
        SPI_I2S_DMACmd(LCD_SPI_SELECT,SPI_I2S_DMAReq_Tx,DISABLE);
        LCD_CS_H
        DMA_ClearFlag(DMA1_FLAG_TC3);//清除标志位
    } else if(LCD_SPI_SELECT==SPI2)
    {
        DMA_Cmd(DMA1_Channel5, DISABLE);
        SPI_I2S_DMACmd(SPI2,SPI_I2S_DMAReq_Tx,ENABLE);
        DMA1_Channel5->CMAR=(uint32_t)data;
        DMA1_Channel5->CPAR=(uint32_t)&(LCD_SPI_SELECT->DR);
        DMA1_Channel5->CNDTR=Data_Len;
        LCD_CS_L
        LCD_DC_H
        DMA_Cmd(DMA1_Channel5, ENABLE);

        while (DMA_GetFlagStatus(DMA1_FLAG_TC5) == RESET);//等待转运完成
        while (SPI_I2S_GetFlagStatus(LCD_SPI_SELECT,SPI_I2S_FLAG_TXE) == RESET);//等待转运完成
        SPI_I2S_DMACmd(LCD_SPI_SELECT,SPI_I2S_DMAReq_Tx,DISABLE);
        LCD_CS_H
        DMA_ClearFlag(DMA1_FLAG_TC5);//清除标志位

    } else if(LCD_SPI_SELECT==SPI3)
    {
        DMA_Cmd(DMA2_Channel2, DISABLE);
        SPI_I2S_DMACmd(SPI3,SPI_I2S_DMAReq_Tx,ENABLE);
        DMA2_Channel2->CMAR=(uint32_t)data;
        DMA2_Channel2->CPAR=(uint32_t)&(LCD_SPI_SELECT->DR);
        DMA2_Channel2->CNDTR=Data_Len;
        LCD_CS_L
        LCD_DC_H
        DMA_Cmd(DMA2_Channel2, ENABLE);

        while (DMA_GetFlagStatus(DMA2_FLAG_TC2) == RESET);//等待转运完成
        while (SPI_I2S_GetFlagStatus(LCD_SPI_SELECT,SPI_I2S_FLAG_TXE) == RESET);//等待转运完成
        SPI_I2S_DMACmd(LCD_SPI_SELECT,SPI_I2S_DMAReq_Tx,DISABLE);
        LCD_CS_H
        DMA_ClearFlag(DMA2_FLAG_TC2);//清除标志位
    }
}

void LCD_GPIO_INIT(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    /*配置通用IO*/
    LCD_Gpio_Rcc_Enable(LCD_CS_PORT);
    LCD_Gpio_Rcc_Enable(LCD_RST_PORT);
    LCD_Gpio_Rcc_Enable(LCD_BK_PORT);
    LCD_Gpio_Rcc_Enable(LCD_DC_PORT);
    GPIO_InitStructure.GPIO_Pin = LCD_CS_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(LCD_CS_PORT, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = LCD_RST_PIN;
    GPIO_Init(LCD_RST_PORT, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = LCD_BK_PIN;
    GPIO_Init(LCD_BK_PORT, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = LCD_DC_PIN;
    GPIO_Init(LCD_DC_PORT, &GPIO_InitStructure);
    LCD_BK_H
    LCD_RST_H
    LCD_CS_H
    LCD_DC_L
#ifdef LCD_SPI //只有spi会有复用8080和qspi没有复用
    /*使能门控*/
    LCD_Gpio_Rcc_Enable(LCD_SPI_MOSI_PORT);
    LCD_Gpio_Rcc_Enable(LCD_SPI_MISO_PORT);
    LCD_Gpio_Rcc_Enable(LCD_SPI_CLK_PORT);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = LCD_SPI_MOSI_PIN;
    GPIO_Init(LCD_SPI_MOSI_PORT, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = LCD_SPI_MISO_PIN;
    GPIO_Init(LCD_SPI_MOSI_PORT, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = LCD_SPI_CLK_PIN;
    GPIO_Init(LCD_SPI_MOSI_PORT, &GPIO_InitStructure);
#endif //LCD_SPI
}


//LCD_SPI_X    SPI1,SPI2,SPI3
void LCD_SPI_INIT(SPI_TypeDef *LCD_SPI_X)
{
    SPI_InitTypeDef  SPI_InitStructure;
    LCD_Spi_Rcc_Enable(LCD_SPI_X);
    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;		//设置SPI工作模式:设置为主SPI
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;		//设置SPI的数据大1小:SPI发送接收8位帧结构
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;		//串行同步时钟的空闲状态为高电平
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;	//串行同步时钟的第二个跳变沿（上升或下降）数据被采样
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;		//NSS信号由硬件（NSS管脚）还是软件（使用SSI位）管理:内部NSS信号有SSI位控制
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;		//定义波特率预分频的值:波特率预分频值为256
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;	//指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
    SPI_InitStructure.SPI_CRCPolynomial = 7;	//CRC值计算的多项式
    SPI_Init(LCD_SPI_X, &SPI_InitStructure);  //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器

    SPI_Cmd(LCD_SPI_X, ENABLE); //使能SPI外设
}

void Dma_Init(void)
{
    DMA_InitTypeDef DMA_InitStructure;
    if(LCD_SPI_SELECT==SPI1)
    {
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
        DMA_DeInit(DMA1_Channel3);
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DR;//外设站点的基地址
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//数据宽度
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//是否自增
        DMA_InitStructure.DMA_MemoryBaseAddr = 0;//存储器站点的基地址
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//传输方向
        DMA_InitStructure.DMA_BufferSize = 0;//传输次数
        DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//是否自动重装
        DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//软件触发
        DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//转运优先级
        DMA_Init(DMA1_Channel3, &DMA_InitStructure);
    } else if(LCD_SPI_SELECT==SPI2)
    {
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
        DMA_DeInit(DMA1_Channel5);
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI2->DR;//外设站点的基地址
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//数据宽度
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//是否自增
        DMA_InitStructure.DMA_MemoryBaseAddr = 0;//存储器站点的基地址
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //数据宽度  字节
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//传输方向
        DMA_InitStructure.DMA_BufferSize = 0;//传输次数
        DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//是否自动重装
        DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//软件触发
        DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//转运优先级
        DMA_Init(DMA1_Channel5, &DMA_InitStructure);
    }  else if(LCD_SPI_SELECT==SPI3)
    {
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
        DMA_DeInit(DMA2_Channel2);
        DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI3->DR;//外设站点的基地址
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//数据宽度
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//是否自增
        DMA_InitStructure.DMA_MemoryBaseAddr = 0;//存储器站点的基地址
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//传输方向
        DMA_InitStructure.DMA_BufferSize = 0;//传输次数
        DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//是否自动重装
        DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//软件触发
        DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//转运优先级
        DMA_Init(DMA2_Channel2, &DMA_InitStructure);
    }
}
void LCD_Rst(void)
{
    LCD_RST_H
    Delay_Ms(10);
    LCD_RST_L;
    Delay_Ms(20);
    LCD_RST_H;
    Delay_Ms(20);
}

/*用于使能spi对应门控的接口，非用户调用*/
void LCD_Spi_Rcc_Enable(SPI_TypeDef *LCD_SPIX)
{
    if(LCD_SPIX==SPI1) {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
    } else if(LCD_SPIX==SPI2) {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
    } else if(LCD_SPIX==SPI3) {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE);
    }
}
/*用于使能控制IO对应门控的接口，非用户调用*/
void LCD_Gpio_Rcc_Enable(GPIO_TypeDef *LCD_GPIOX)
{
    if(LCD_GPIOX==GPIOA) {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
    } else if(LCD_GPIOX==GPIOB) {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
    } else if(LCD_GPIOX==GPIOC) {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
    } else if(LCD_GPIOX==GPIOD) {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
    } else if(LCD_GPIOX==GPIOE) {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
    } else if(LCD_GPIOX==GPIOF) {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
    }
}
void LCD_Reg_Config(void)
{
#ifdef LCD_ST7796
    ST7796_REG_Config();
#endif //LCD_ST7796
#ifdef LCD_GC9A91
    GC9A91_REG_Config();
#endif //LCD_GC9A91
#ifdef LCD_ST7789S
	ST7789S_REG_Config();
#endif //LCD_ST7789S
}
#ifdef LCD_ST7796
void ST7796_REG_Config ( void )
{
    LCD_Write_Cmd(0x11); 			//Sleep Out
    Delay_Ms(120);               //DELAY120ms
    //--------------------------------ST7796 Frame rate setting----------------------------------//
    LCD_Write_Cmd(0Xf0);
    LCD_Write_Sing_Data(0xc3);

    LCD_Write_Cmd(0Xf0);
    LCD_Write_Sing_Data(0x96);

    LCD_Write_Cmd(0X36);
    LCD_Write_Sing_Data(0x48);

    LCD_Write_Cmd(0X3a);
    LCD_Write_Sing_Data(0x05);

    LCD_Write_Cmd(0XB4);
    LCD_Write_Sing_Data(0x01);

    LCD_Write_Cmd(0Xe6);
    LCD_Write_Sing_Data(0x0f);
    LCD_Write_Sing_Data(0xf2);
    LCD_Write_Sing_Data(0x3f);
    LCD_Write_Sing_Data(0x4f);
    LCD_Write_Sing_Data(0x4f);
    LCD_Write_Sing_Data(0x28);
    LCD_Write_Sing_Data(0x0e);
    LCD_Write_Sing_Data(0x00);

    LCD_Write_Cmd(0Xc5);
    LCD_Write_Sing_Data(0x20);

    LCD_Write_Cmd(0Xe0);
    LCD_Write_Sing_Data(0xf0);
    LCD_Write_Sing_Data(0x03);
    LCD_Write_Sing_Data(0x0a);
    LCD_Write_Sing_Data(0x11);
    LCD_Write_Sing_Data(0x14);
    LCD_Write_Sing_Data(0x1c);
    LCD_Write_Sing_Data(0x3b);
    LCD_Write_Sing_Data(0x55);
    LCD_Write_Sing_Data(0x4a);
    LCD_Write_Sing_Data(0x0a);
    LCD_Write_Sing_Data(0x13);
    LCD_Write_Sing_Data(0x14);
    LCD_Write_Sing_Data(0x1c);
    LCD_Write_Sing_Data(0x1f);

    LCD_Write_Cmd(0Xe1);
    LCD_Write_Sing_Data(0xf0);
    LCD_Write_Sing_Data(0x03);
    LCD_Write_Sing_Data(0x0a);
    LCD_Write_Sing_Data(0x0c);
    LCD_Write_Sing_Data(0x0c);
    LCD_Write_Sing_Data(0x09);
    LCD_Write_Sing_Data(0x36);
    LCD_Write_Sing_Data(0x54);
    LCD_Write_Sing_Data(0x49);
    LCD_Write_Sing_Data(0x0f);
    LCD_Write_Sing_Data(0x1b);
    LCD_Write_Sing_Data(0x18);
    LCD_Write_Sing_Data(0x1b);
    LCD_Write_Sing_Data(0x1f);

    LCD_Write_Cmd(0Xf0);
    LCD_Write_Sing_Data(0x3c);

    LCD_Write_Cmd(0Xf0);
    LCD_Write_Sing_Data(0x69);
    Delay_Ms(120);
    LCD_Write_Cmd(0X29);
}
#endif //LCD_ST7796
#ifdef LCD_GC9A91
void GC9A91_REG_Config (void)
{
    LCD_Write_Cmd(0xFE);
    LCD_Write_Cmd(0xEF);
    LCD_Write_Cmd(0xEB);
    LCD_Write_Sing_Data(0x14);
    LCD_Write_Cmd(0x84);
    LCD_Write_Sing_Data(0x40);
    LCD_Write_Cmd(0x88);
    LCD_Write_Sing_Data(0x0A);
    LCD_Write_Cmd(0x89);
    LCD_Write_Sing_Data(0x21);
    LCD_Write_Cmd(0x8A);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Cmd(0x8B);
    LCD_Write_Sing_Data(0x80);
    LCD_Write_Cmd(0x8C);
    LCD_Write_Sing_Data(0x01);
    LCD_Write_Cmd(0x8D);
    LCD_Write_Sing_Data(0x03);
    LCD_Write_Cmd(0x8F);
    LCD_Write_Sing_Data(0xFF);
    LCD_Write_Cmd(0xB6);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(0x60);
    LCD_Write_Cmd(0x36);
    LCD_Write_Sing_Data(0x88);
    LCD_Write_Cmd(0x3A);
    LCD_Write_Sing_Data(0x05);
    LCD_Write_Cmd(0x90);
    LCD_Write_Sing_Data(0x08);
    LCD_Write_Sing_Data(0x08);
    LCD_Write_Sing_Data(0x08);
    LCD_Write_Sing_Data(0x08);
    LCD_Write_Cmd(0xBD);
    LCD_Write_Sing_Data(0x06);
    LCD_Write_Cmd(0xBC);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Cmd(0xFF);
    LCD_Write_Sing_Data(0x60);
    LCD_Write_Sing_Data(0x01);
    LCD_Write_Sing_Data(0x04);
    LCD_Write_Cmd(0xC3);
    LCD_Write_Sing_Data(0x2F);
    LCD_Write_Cmd(0xC4);
    LCD_Write_Sing_Data(0x2F);
    LCD_Write_Cmd(0xC9);
    LCD_Write_Sing_Data(0x25);
    LCD_Write_Cmd(0xBE);
    LCD_Write_Sing_Data(0x11);
    LCD_Write_Cmd(0xE1);
    LCD_Write_Sing_Data(0x10);
    LCD_Write_Sing_Data(0x0E);
    LCD_Write_Cmd(0xDF);
    LCD_Write_Sing_Data(0x21);
    LCD_Write_Sing_Data(0x10);
    LCD_Write_Sing_Data(0x02);
    LCD_Write_Cmd(0xF0);
    LCD_Write_Sing_Data(0x49);
    LCD_Write_Sing_Data(0x0e);
    LCD_Write_Sing_Data(0x09);
    LCD_Write_Sing_Data(0x09);
    LCD_Write_Sing_Data(0x25);
    LCD_Write_Sing_Data(0x2e);
    LCD_Write_Cmd(0xF1);
    LCD_Write_Sing_Data(0x44);
    LCD_Write_Sing_Data(0x70);
    LCD_Write_Sing_Data(0x73);
    LCD_Write_Sing_Data(0x2F);
    LCD_Write_Sing_Data(0x30);
    LCD_Write_Sing_Data(0x6F);
    LCD_Write_Cmd(0xF2);
    LCD_Write_Sing_Data(0x49);
    LCD_Write_Sing_Data(0x0e);
    LCD_Write_Sing_Data(0x09);
    LCD_Write_Sing_Data(0x09);
    LCD_Write_Sing_Data(0x25);
    LCD_Write_Sing_Data(0x2e);
    LCD_Write_Cmd(0xF3);
    LCD_Write_Sing_Data(0x44);
    LCD_Write_Sing_Data(0x70);
    LCD_Write_Sing_Data(0x73);
    LCD_Write_Sing_Data(0x2F);
    LCD_Write_Sing_Data(0x30);
    LCD_Write_Sing_Data(0x6F);
    LCD_Write_Cmd(0xED);
    LCD_Write_Sing_Data(0x1B);
    LCD_Write_Sing_Data(0x8B);
    LCD_Write_Cmd(0xAE);
    LCD_Write_Sing_Data(0x77);
    LCD_Write_Cmd(0xCD);
    LCD_Write_Sing_Data(0x63);
    LCD_Write_Cmd(0xAC);
    LCD_Write_Sing_Data(0x27);
    LCD_Write_Cmd(0x70);
    LCD_Write_Sing_Data(0x07);
    LCD_Write_Sing_Data(0x07);
    LCD_Write_Sing_Data(0x04);
    LCD_Write_Sing_Data(0x06);//VGH
    LCD_Write_Sing_Data(0x0F); //VGL
    LCD_Write_Sing_Data(0x09);
    LCD_Write_Sing_Data(0x07);
    LCD_Write_Sing_Data(0x08);
    LCD_Write_Sing_Data(0x03);
    LCD_Write_Cmd(0xE8);
    LCD_Write_Sing_Data(0x24);
    LCD_Write_Cmd(0x60);
    LCD_Write_Sing_Data(0x38);
    LCD_Write_Sing_Data(0x0B);
    LCD_Write_Sing_Data(0x6D);
    LCD_Write_Sing_Data(0x6D);
    LCD_Write_Sing_Data(0x39);
    LCD_Write_Sing_Data(0xF0);
    LCD_Write_Sing_Data(0x6D);
    LCD_Write_Sing_Data(0x6D);
    LCD_Write_Cmd(0x61);
    LCD_Write_Sing_Data(0x38);
    LCD_Write_Sing_Data(0xF4);
    LCD_Write_Sing_Data(0x6D);
    LCD_Write_Sing_Data(0x6D);
    LCD_Write_Sing_Data(0x38);
    LCD_Write_Sing_Data(0xF7);
    LCD_Write_Sing_Data(0x6D);
    LCD_Write_Sing_Data(0x6D);/////////////////////////////////
    LCD_Write_Cmd(0x62);
    LCD_Write_Sing_Data(0x38);
    LCD_Write_Sing_Data(0x0D);
    LCD_Write_Sing_Data(0x71);
    LCD_Write_Sing_Data(0xED);
    LCD_Write_Sing_Data(0x70);
    LCD_Write_Sing_Data(0x70);
    LCD_Write_Sing_Data(0x38);
    LCD_Write_Sing_Data(0x0F);
    LCD_Write_Sing_Data(0x71);
    LCD_Write_Sing_Data(0xEF);
    LCD_Write_Sing_Data(0x70);
    LCD_Write_Sing_Data(0x70);
    LCD_Write_Cmd(0x63);
    LCD_Write_Sing_Data(0x38);
    LCD_Write_Sing_Data(0x11);
    LCD_Write_Sing_Data(0x71);
    LCD_Write_Sing_Data(0xF1);
    LCD_Write_Sing_Data(0x70);
    LCD_Write_Sing_Data(0x70);
    LCD_Write_Sing_Data(0x38);
    LCD_Write_Sing_Data(0x13);
    LCD_Write_Sing_Data(0x71);
    LCD_Write_Sing_Data(0xF3);
    LCD_Write_Sing_Data(0x70);
    LCD_Write_Sing_Data(0x70);
    LCD_Write_Cmd(0x64);
    LCD_Write_Sing_Data(0x28);
    LCD_Write_Sing_Data(0x29);
    LCD_Write_Sing_Data(0xF1);
    LCD_Write_Sing_Data(0x01);
    LCD_Write_Sing_Data(0xF1);
    LCD_Write_Sing_Data(0x00);//
    LCD_Write_Sing_Data(0x1a);//
    LCD_Write_Cmd(0x66);
    LCD_Write_Sing_Data(0x3C);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(0x98);
    LCD_Write_Sing_Data(0x10);
    LCD_Write_Sing_Data(0x32);
    LCD_Write_Sing_Data(0x45);
    LCD_Write_Sing_Data(0x01);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Cmd(0x67);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(0x3C);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(0x10);
    LCD_Write_Sing_Data(0x54);
    LCD_Write_Sing_Data(0x67);
    LCD_Write_Sing_Data(0x45);
    LCD_Write_Sing_Data(0xcd);
    LCD_Write_Cmd(0x74);
    LCD_Write_Sing_Data(0x10);
    LCD_Write_Sing_Data(0x85); //85
    LCD_Write_Sing_Data(0x80);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(0x4E);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Cmd(0x98);
    LCD_Write_Sing_Data(0x3e);
    LCD_Write_Sing_Data(0x07);
    LCD_Write_Cmd(0x99);
    LCD_Write_Sing_Data(0x3e);
    LCD_Write_Sing_Data(0x07);
    LCD_Write_Cmd(0x35);
    LCD_Write_Cmd(0x21);
    Delay_Ms(120);
//--------end gamma setting--------------//
    LCD_Write_Cmd(0x11);
    Delay_Ms(320);
    LCD_Write_Cmd(0x29);
    Delay_Ms(120);
    LCD_Write_Cmd(0x2C);
}
#endif //LCD_GC9A91

#ifdef LCD_ST7789S
void ST7789S_REG_Config(void)
{
    //--------------------------------------Display Setting------------------------------------------//
    LCD_Write_Cmd(0x11); // Exit Sleep
    Delay_Ms(120);          // Delay 120ms
    #if 0
    LCD_Write_Cmd(0x36); // 设置内存扫描方向，0X00正常扫描，从上往下，从左往右，RGB方式
//	LCD_Write_Sing_Data(0x00); // 4中不同模式选一
//	LCD_Write_Sing_Data(0xC0);
//	LCD_Write_Sing_Data(0x70);
//	LCD_Write_Sing_Data(0xA0);
    #endif

    LCD_Write_Cmd(0x3a); // 数据格式，65K色,565
    LCD_Write_Sing_Data(0x05);

    LCD_Write_Cmd(0x21); // 反显开关，0X20反显模式，  0x21正常模式
    //--------------------------------ST7789S Frame rate setting-------------------------

    LCD_Write_Cmd(0xb2); // 帧频设置
    LCD_Write_Sing_Data(0x05);
    LCD_Write_Sing_Data(0x05);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(0x33);
    LCD_Write_Sing_Data(0x33);
    LCD_Write_Cmd(0xb7); // GATE 设置
    LCD_Write_Sing_Data(0x35);
    //---------------------------------ST7789S Power setting-----------------------------
    LCD_Write_Cmd(0xb8);
    LCD_Write_Sing_Data(0x2f);
    LCD_Write_Sing_Data(0x2b);
    LCD_Write_Sing_Data(0x2f);
    LCD_Write_Cmd(0xbb); // VCOM设置
    LCD_Write_Sing_Data(0x20);
    LCD_Write_Cmd(0xc0); // LCM设置,默认0x2c
    LCD_Write_Sing_Data(0x2c);
    LCD_Write_Cmd(0xc2); // VDV&VRH SET ,默认0x01
    LCD_Write_Sing_Data(0x01);
    LCD_Write_Cmd(0xc3); // VRHS SET，默认0x0b
    LCD_Write_Sing_Data(0x0b);
    LCD_Write_Cmd(0xc4); // VDV SET，默认0x20
    LCD_Write_Sing_Data(0x20);
    LCD_Write_Cmd(0xc6); // FR SET, 默认0x0F
    LCD_Write_Sing_Data(0x01);
    LCD_Write_Cmd(0xd0); // 电源控制1
    LCD_Write_Sing_Data(0xa4);
    LCD_Write_Sing_Data(0xa1);
    LCD_Write_Cmd(0xe8);
    LCD_Write_Sing_Data(0x03);
    LCD_Write_Cmd(0xe9);
    LCD_Write_Sing_Data(0x0d);
    LCD_Write_Sing_Data(0x12);
    LCD_Write_Sing_Data(0x00);
    //--------------------------------ST7789S gamma setting------------------------------
    LCD_Write_Cmd(0xe0);
    LCD_Write_Sing_Data(0xd0);
    LCD_Write_Sing_Data(0x06);
    LCD_Write_Sing_Data(0x0b);
    LCD_Write_Sing_Data(0x0a);
    LCD_Write_Sing_Data(0x09);
    LCD_Write_Sing_Data(0x05);
    LCD_Write_Sing_Data(0x2e);
    LCD_Write_Sing_Data(0x43);
    LCD_Write_Sing_Data(0x44);
    LCD_Write_Sing_Data(0x09);
    LCD_Write_Sing_Data(0x16);
    LCD_Write_Sing_Data(0x15);
    LCD_Write_Sing_Data(0x23);
    LCD_Write_Sing_Data(0x27);
    LCD_Write_Cmd(0xe1);
    LCD_Write_Sing_Data(0xd0);
    LCD_Write_Sing_Data(0x06);
    LCD_Write_Sing_Data(0x0b);
    LCD_Write_Sing_Data(0x09);
    LCD_Write_Sing_Data(0x08);
    LCD_Write_Sing_Data(0x06);
    LCD_Write_Sing_Data(0x2e);
    LCD_Write_Sing_Data(0x44);
    LCD_Write_Sing_Data(0x44);
    LCD_Write_Sing_Data(0x3a);
    LCD_Write_Sing_Data(0x15);
    LCD_Write_Sing_Data(0x15);
    LCD_Write_Sing_Data(0x23);
    LCD_Write_Sing_Data(0x26);
    //----------------------RGB set------------------//
    LCD_Write_Cmd(0x35);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Cmd(0x44);
    LCD_Write_Sing_Data(0x00);
    LCD_Write_Sing_Data(10);    // 调切屏
    LCD_Write_Cmd(0x29); // Display on
}
#endif
